package com.hejia.bulbul.scheduler;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;

import com.alibaba.fastjson.JSONObject;
import com.hejia.alauda.entity.WarningSetting;
import com.hejia.alauda.service.ConfigureService;
import com.hejia.alauda.service.PaymentService;
import com.hejia.alauda.utils.LLPayUtil;

/**
 * 银行失败检查预警作业
 * 每5分钟巡检一次
 * @author hyr
 *
 */
public class BankCheckWarningScheduler {
	
	private static final Log logger = LogFactory.getLog(BankCheckWarningScheduler.class);
	
	@Resource
	private ConfigureService warningService;
	
	@Resource
	private PaymentService paymentService;
	
	// [秒] [分] [小时] [日] [月] [周] [年]
	@Scheduled(cron = "0 0/5 * * * ?")
	@Async("executor")
	public void check(){

		logger.debug("开始银行失败检查预警作业");
		
		WarningSetting warningSetting = this.warningService.getWarningSetting();
		if(warningSetting == null || !warningSetting.getBankSwitch()){
			return;
		}
		
		warningService.checkFailOrderBanks();
		
		logger.debug("结束银行失败检查预警作业");
	}
	
	/**
	 * 同步银行状态和限额
	 * 每10分钟同步一次
	 * @Scheduled(fixedDelay = 10 * 60 * 1000)
	 */
	
	public void syncBankStatusAndLimit() {
		
		logger.info("开始同步银行状态和限额");

		JSONObject bankJson = LLPayUtil.supportBankQuery();
		if (bankJson == null) {
			logger.error("查询连连银行状态和限额失败");
			return;
		}
		if (!bankJson.getString("ret_code").equals("0000")) {
			logger.error("查询银行状态和限额：" + bankJson.getString("ret_msg"));
			return;
		}

		this.paymentService.syncLLPayBankStatusAndLimit(bankJson.getJSONArray("support_banklist"));

		logger.info("结束同步银行状态和限额");
	}
}
